package com.china_goldcard.myselectview;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

/**
 * Created by jk on 16/10/27.
 */
public class MySelectViewAdapter<M extends MySelectItemModel,V extends MySelectViewHolder> extends RecyclerView.Adapter<V> {

    private List<M> dataList;

    private float itemH;

    public interface OnItemClickListener<M extends MySelectItemModel>{
        void onItemClick(View v,M model);
        void onLongItemClick(View v,M model);
    }

    private OnItemClickListener onItemClickListener;

    private MySelectDataAdapter<V,M> dataAdapter;

    public MySelectViewAdapter(List<M> dataList,MySelectDataAdapter<V,M>dataAdapter){
        this.dataList = dataList;
        this.dataAdapter = dataAdapter;
    }

    @Override
    public V onCreateViewHolder(ViewGroup parent, int viewType) {
        if (dataAdapter!=null){
            V viewHolder = dataAdapter.onCreateViewHolder(parent,viewType);
            if (viewHolder.itemView!=null){
                viewHolder.itemView.getLayoutParams().height = (int)itemH;
            }
            return viewHolder;
        }else{
            return null;
        }
    }

    @Override
    public void onBindViewHolder(V holder, final int position) {
        if (dataAdapter!=null) {
            dataAdapter.onBindViewHolder(holder, position, dataList.get(position));
        }
        if (onItemClickListener!=null && holder!=null && holder.itemView!=null){
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.onItemClick(v,dataList.get(position));
                    for(MySelectItemModel item : dataList){
                        item.setSelected(false);
                    }
                    dataList.get(position).setSelected(true);
                    notifyDataSetChanged();
                }
            });
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    onItemClickListener.onLongItemClick(v,dataList.get(position));
                    for(MySelectItemModel item : dataList){
                        item.setSelected(false);
                    }
                    dataList.get(position).setSelected(true);
                    notifyDataSetChanged();
                    return false;
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    public void setDataAdapter(MySelectDataAdapter<V, M> dataAdapter) {
        this.dataAdapter = dataAdapter;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void setItemH(float itemH) {
        this.itemH = itemH;
    }
}
